package com.bo.gather.data;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.bo.WeiboConfig;
import com.bo.util.BatchRenamefile;
import com.bo.util.IOUtil;
import com.bo.weibo.bean.SendDataInfo;
import com.bo.weibo.bean.WeiboSendBean;


public class SearchRepeatData {
	String root = WeiboConfig.getValue("gatherRepeatData").trim();// "D:\\bo\\data\\";//文件夹目录
	
	public void  cleanRepeatData(String filename,String onlyid,List<Map<String,String>> gatherList){
		List<String> list=new ArrayList<String>();
		for(Map<String,String> map:gatherList){
			list.add(map.get(onlyid));
		}
		
		List<String> removelist=new ArrayList<String>();
		
		File file=new File(root+filename);
		if(file.exists()){
			BufferedReader in = null;
			String currLine = null;
			try {
				InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "UTF-8");   
				in = new BufferedReader(isr);
				while ((currLine = in.readLine()) != null) {
					if (currLine.trim().equals("")) {
						continue;
					}
//					String []array=currLine.split("@@");
					
					Iterator<String> iterator= list.iterator();
					while(iterator.hasNext()){
						String ss=iterator.next();
						if(currLine.equals(ss)){
							removelist.add(ss);
							iterator.remove();
						}
					}
				}	
			}catch(Exception io){
				io.printStackTrace();
			} finally {
				try {
					in.close();
				} catch (IOException ex) {				
				}
				in = null;
			}
			
			//踢出重复的
			Iterator<Map<String,String>> iteratormap=gatherList.iterator();
			while(iteratormap.hasNext()){
				Map<String,String> map=iteratormap.next();
				String onlystr=map.get(onlyid);
				
				for(String s:removelist){
					if(s.equals(onlystr)){
						iteratormap.remove();
						break;
					}
				}
			}
		}
		
		
		//剩下不重复的写到文件中
		if(gatherList.size()>0){
			StringBuffer sb=new StringBuffer();
			for(Map<String,String> map:gatherList){
//				Date date=new Date();
				sb.append(map.get(onlyid));
//				sb.append("@@");
//				sb.append(date.getTime());
				sb.append("\r\n");
			}
			IOUtil.writeEndData(file,  sb.toString(), "UTF-8");
		}
		
	}
	
	public void  cleanRepeatData(WeiboSendBean weibo){
//		List<String> list=new ArrayList<String>();
//		for(Map<String,String> map:gatherList){
//			list.add(map.get(onlyid));
//		}
		
		
		File file=new File(root+weibo.getAccessInfo().getNick());
		if(file.exists()){
			BufferedReader in = null;
			String currLine = null;
			try {
				InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "UTF-8");   
				in = new BufferedReader(isr);
				while ((currLine = in.readLine()) != null) {
					if (currLine.trim().equals("")) {
						continue;
					}
					
					Iterator<SendDataInfo> iterator= weibo.getSendDataList().iterator();
					while(iterator.hasNext()){
						SendDataInfo ss=iterator.next();
						if(currLine.equals(ss.getOnlyid())){
							iterator.remove();
						}
					}
				}	
			}catch(Exception io){
				io.printStackTrace();
			} finally {
				try {
					in.close();
				} catch (IOException ex) {				
				}
				in = null;
			}
		}
		

		
	}
	
	public boolean  isRepeatData(String filename,SendDataInfo sendDataInfo){
		
		
		File file=new File(root+filename);
		if(file.exists()){
			BufferedReader in = null;
			String currLine = null;
			try {
				InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "UTF-8");   
				in = new BufferedReader(isr);
				while ((currLine = in.readLine()) != null) {
					if (currLine.trim().equals("")) {
						continue;
					}
					if(currLine.equals(sendDataInfo.getOnlyid())){
						return false;
					}
				}	
			}catch(Exception io){
				io.printStackTrace();
			} finally {
				try {
					in.close();
				} catch (IOException ex) {				
				}
				in = null;
			}
		}
		
		
		//剩下不重复的写到文件中
		if(IOUtil.writeEndData(file, sendDataInfo.getOnlyid()+"\r\n", "UTF-8")){
			return true;
		}
		return false;
	}
	
	
	
//	public void writeDataLog(String gatherid,String onlyinfo){
//		FileWriter fw=null;
//		try {
//			fw = new FileWriter(root+gatherid,true);
//			fw.write(new String(onlyinfo.getBytes("utf-8")));
//		} catch (IOException e) {
//			e.printStackTrace();
//		}finally{
//			try {
//				fw.close();
//			} catch (IOException e) {
//				e.printStackTrace();
//			}
//		}
//	}
	/**
	 * 定时删除数据，每格7天删除一次
	 * @throws IOException
	 */
	public void cleanOldData() throws IOException{
		List<File> filelist=this.getAllFile();
		for(File file:filelist){
			String filestr=IOUtil.readFileToString(file, "utf-8");
			String [] array=filestr.split("\r\t");
			int temp=array.length-100>=0?array.length-100:0;
			StringBuffer sb=new StringBuffer();
			for(int i=temp;i<array.length;i++){
				sb.append(array[i]);
				sb.append("\r\n");
			}
			IOUtil.writeStringToFile(file, sb.toString(), "utf-8");
		}
	}
	private List<File> getAllFile(){
		
		  List<File> filelist=new ArrayList<File>();
		  try {
			BatchRenamefile.readfile(root,filelist);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		return filelist;
	}
	public static void main(String[] args)   {
		SearchRepeatData sd=new SearchRepeatData();
//		sd.writeDataLog("bo", "eeeeeeeeee\r\n");
		
		List<Map<String,String>> gatherList=new ArrayList<Map<String,String>>();
		Map<String,String> temp1=new HashMap<String,String>();
		temp1.put("id", "KK");
		temp1.put("444", "dddd");
		
		Map<String,String> temp2=new HashMap<String,String>();
		temp2.put("id", "3333");
		temp2.put("444", "3322");
		
		gatherList.add(temp1);
		gatherList.add(temp2);
		
		sd.cleanRepeatData("bo", "id", gatherList);
		for(Map<String,String> map:gatherList){
			System.out.println(map.toString());
		}
	}
}
